package ac.neec.mio.ui.activity;

import ac.neec.db.service.SQLManager;
import ac.neec.listener.ProgressListener;
import ac.neec.mio.R;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity implements ProgressListener {

	private static final int METS_INSERT_VERSION = 0;
	private Button profileButton;
	private Button settingButton;
	private Button measurementButton;
	private Button recordButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initSQLDatabase();

		initFindViews();
		setListener();
		
		getActionBar().setTitle("NeecMio β");
	}

	private void initSQLDatabase() {
		new SQLManager(this);
		SharedPreferences pref = getSharedPreferences(getResources().getString(R.string.pref_name), MODE_PRIVATE);
		int version = pref.getInt(getResources().getString(R.string.pref_cullom_insert_ver), -1);
		if (version < METS_INSERT_VERSION) {
			SQLManager.execInsert(this);
			Editor edit = pref.edit();
			edit.putInt(getResources().getString(R.string.pref_cullom_insert_ver), METS_INSERT_VERSION);
			edit.commit();
		}
	}

	private void initFindViews() {
		measurementButton = (Button) findViewById(R.id.btn_measurement);
		recordButton = (Button) findViewById(R.id.btn_record);
		profileButton = (Button) findViewById(R.id.btn_profile);
		settingButton = (Button) findViewById(R.id.btn_setting_test);
	}

	private void setListener() {
		measurementButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				intentMeasurement();
			}
		});
		recordButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				intentRecord();
			}
		});
		profileButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				intentProfile();
			}
		});
		settingButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				intentSetting();
			}
		});
	}

	private void intentMeasurement() {
		Intent intent = new Intent(getApplicationContext(), MeasurementSettingActivity.class);
		startActivity(intent);
	}

	private void intentRecord() {
		Intent intent = new Intent(getApplicationContext(), RecordActivity.class);
		startActivity(intent);

	}

	private void intentSetting() {
		Intent intent = new Intent(getApplicationContext(), MeasurementTestActivity.class);
		startActivity(intent);
	}

	private void intentProfile() {
		Intent intent = new Intent(getApplicationContext(), ProfileActivity.class);
		startActivity(intent);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		switch (item.getItemId()) {
		case R.id.action_settings:
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void notofyMaxNumeric(int num) {
		// TODO 自動生成されたメソッド・スタブ

	}

	@Override
	public void notifyNowNumeric(int num) {
		// TODO 自動生成されたメソッド・スタブ

	}

	@Override
	public void notifyComplete() {
		// TODO 自動生成されたメソッド・スタブ

	}

}
